/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.service;

import ac.pbp.core.exception.ServiceException;
import ac.pbp.core.filter.FilterCriteria;
import ac.pbp.core.paging.FilterAndPaging;
import ac.pbp.dao.UserDao;
import ac.pbp.dto.UserDto;
import ac.pbp.dto.filter.UserFilterCriteria;
import ac.pbp.model.User;
import ac.pbp.model.User;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Kim
 */
@Component
@Transactional
public class UserService {
    
    @Autowired
    private UserDao userDao;
    
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public void addUser(User user) throws ServiceException {
                
        if (user.getKodeUser() != null) {
        
            userDao.add(user);
        } 
    }
    
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public String updateUser(User user) throws ServiceException {
        
        if (user.getKodeUser() != null) {
            
            User userInner = userDao.load(user.getKodeUser());
            userInner.setDisplayName(user.getDisplayName());
            userInner.setEmailuser(user.getEmailuser());
            userInner.setFirstName(user.getFirstName());
            userInner.setGender(user.getGender());
            userInner.setLastName(user.getLastName());
            userInner.setPasswordUser(user.getPasswordUser());
            userInner.setPhoto(user.getPhoto());
            userDao.update(userInner);  
        }
        
        return user.getKodeUser();
    }
    
    @Transactional(readOnly = true, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public User loadUser(String kodeUser) {
        return userDao.load(kodeUser);
    }
    
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public void deleteUser(String idArray) {
        String[] idArrayRay = idArray.split(",");
        for (int i = 0; i < idArrayRay.length; i++) {
            try {
                if (!idArrayRay[i].equals("")) {
                    userDao.delete(userDao.load(idArrayRay[i]));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    public UserDto filterUser(FilterCriteria filterCriteria) {
        UserDto userDto = new UserDto();
        
        userDto.setUserList(FilterAndPaging.filter(this.userDao, filterCriteria));
        userDto.setFilterCriteria((UserFilterCriteria) filterCriteria);

        return userDto;
    }
    
    public User checkAdmin(String email, String password) {
        return userDao.checkAdmin(email, password);
    
    }
    public User checkUser(String email, String password) {
        return userDao.checkUser(email, password);
    }
    
    public Long getMax() {
        return userDao.getMax();
    }
    
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = {Exception.class})
    public String updateUserInterest(User user, String kodeUser) throws ServiceException {
        
        if (kodeUser != null) {
            
            User userInner = userDao.load(kodeUser);
            userInner.setInterestedGenre(user.getInterestedGenre());
            userDao.update(userInner);  
        }
        
        return user.getKodeUser();
    }
    
    public List<User> loadAll() {
        
        return userDao.loadAll();
    }
}
